package net.maku.module.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fhs.common.utils.CheckUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.TreeUtils;
import net.maku.module.business.convert.DictionariesSubsetConvert;
import net.maku.module.business.entity.DictionariesEntity;
import net.maku.module.business.entity.DictionariesSubsetEntity;
import net.maku.module.business.service.DictionariesService;
import net.maku.module.business.service.DictionariesSubsetService;
import net.maku.module.business.vo.DictionariesSubsetVO;
import net.maku.newbase.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.RegEx;
import java.util.List;

/**
 * 业务_字典子类
 *
 * @author
 */
@RestController
@RequestMapping("nb/business/dictionaries_subset")
@Tag(name = "业务_字典子类")
@AllArgsConstructor
public class DictionariesSubsetController extends NewBaseController<DictionariesSubsetService, DictionariesSubsetEntity> {

    @Autowired
    protected DictionariesService dictionariesService;

    /**
     * 查询 根据父id
     *
     * @param vo
     */
    @Operation(summary = "查询 根据父id")
    @RequestMapping(value = "findByDictId", method = RequestMethod.PUT)
    public R findByPid(@RequestBody DictionariesSubsetVO vo) {
        if (!CheckUtils.isNotEmpty(vo.getDictId())) {
            return R.error(RE.PARAM_ERR);
        }

        return R.ok(entityService.findByDictId(vo.getDictId()));
    }

    /**
     * 分页
     *
     * @param page
     * @param query
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public R page(PageParam page, NewQuery query, DictionariesSubsetEntity entity) {
        LambdaQueryWrapper<DictionariesSubsetEntity> select = new LambdaQueryWrapper<>();
        select.eq(DictionariesSubsetEntity::getDictId, entity.getDictId());
        select.orderByAsc(DictionariesSubsetEntity::getSort);

        IPage<DictionariesSubsetEntity> iPage = entityService.page(page.b(), select);
        return R.ok(new PageResult<>(DictionariesSubsetConvert.INSTANCE.convertList(iPage.getRecords()), iPage.getTotal()));
    }

    /**
     * 列表
     *
     * @param page
     * @param query
     * @return
     */
    @Operation(summary = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public R list(PageParam page, NewQuery query, DictionariesSubsetEntity entity) {
        if (StringUtils.isNotBlank(query.getCode())) {
            DictionariesEntity dictionariesEntity = dictionariesService.findByCode(query.getCode());
            entity.setDictId(dictionariesEntity.getId());
        }

        LambdaQueryWrapper<DictionariesSubsetEntity> select = new LambdaQueryWrapper<>();
        select.eq(DictionariesSubsetEntity::getDictId, entity.getDictId());
        select.orderByAsc(DictionariesSubsetEntity::getSort);

        List<DictionariesSubsetEntity> list = entityService.list(select);

        return R.ok(TreeUtils.build(DictionariesSubsetConvert.INSTANCE.convertList(list), Constant.ROOT));
    }

    @RegEx
    @Operation(summary = "查询")
    @RequestMapping(value = "{code}", method = RequestMethod.GET)
    public R delete(@PathVariable("code") String code) {
        DictionariesEntity dictionariesEntity = dictionariesService.findByCode(code);
        if (dictionariesEntity == null) {
            return R.ok();
        }

        DictionariesSubsetEntity entity = new DictionariesSubsetEntity();
        entity.setDictId(dictionariesEntity.getId());
        return page(new PageParam(), null, entity);
    }
}